﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EpiNEXT.WPF
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:EpiNEXT.WPF"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:EpiNEXT.WPF;assembly=EpiNEXT.WPF"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:ElementDesignBorder/>
    ///
    /// </summary>
    public class ElementDesignBorder : ContentControl
    {
        static ElementDesignBorder()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ElementDesignBorder), new FrameworkPropertyMetadata(typeof(ElementDesignBorder)));
        }

        protected override Geometry GetLayoutClip(Size layoutSlotSize)
        {
            return ClipToBounds ? base.GetLayoutClip(layoutSlotSize) : null;
        }

        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(ElementDesignBorder), new FrameworkPropertyMetadata(false, OnIsSelectedChanged));
        public bool IsSelected
        {
            get
            {
                return (bool)(this.GetValue(IsSelectedProperty));
            }
            set
            {
                this.SetValue(IsSelectedProperty, value);
            }
        }

        public static void OnIsSelectedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ElementDesignBorder border = obj as ElementDesignBorder;
            if (border != null)
            {
                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(border);
                Adorner[] adorners = adornerLayer.GetAdorners(border);

                if (border.IsSelected)
                {
                    foreach (Adorner adorner in adorners)
                    {
                        adorner.Visibility = Visibility.Visible;
                    }
                }
                else
                {
                    foreach (Adorner adorner in adorners)
                    {
                        adorner.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        public event EventHandler Drag;
        public event EventHandler DragStart;
        public event EventHandler DragStop;

        private bool IsBeingDragged = false;

        public FrameworkElement GetContainedElement()
        {
            FrameworkElement element = this.Content as FrameworkElement;
            if (element != null)
            {
                return element;
            }

            return null; // should never get here
        }

        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            base.OnPreviewMouseMove(e);

            if (IsBeingDragged && IsSelected)
            {
                if (this.Drag != null)
                {
                    Drag(this, new EventArgs());
                }
            }
        }

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);

            IsSelected = !IsSelected;

            IsBeingDragged = false;
            if (this.DragStop != null)
            {
                DragStop(this, new EventArgs());
            }
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            if (IsSelected)
            {
                IsBeingDragged = true;

                if (this.DragStart != null)
                {
                    DragStart(this, new EventArgs());
                }
            }
        }
    }
}
